﻿using System;
using System.IO;
using QuickTuner;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;




namespace QuickTuner
{


    public partial class editForm : Form
    {
        private StationList _list;

        public static bool InstanceRunning { get; set; }

        public editForm(StationList theList)
        {
            InstanceRunning = true;

            InitializeComponent();

            stationsSource.DataSource = _list = theList;
        }

        // Event handlers _______________________________________________________________

        private void addButton_Click(object sender, EventArgs e)
        {
            stationsSource.AddNew();
        }
        private void deleteButton_Click(object sender, EventArgs e)
        {
            stationsSource.RemoveCurrent();
        }

        private void importListButton_Click(object sender, EventArgs e)
        {
            // load saved path or default to my documents
            var path = QuickTuner.Properties.Settings.Default.lastOpenPath;
            if (string.IsNullOrEmpty(path))
                path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            openDialog.InitialDirectory = path;

            // show the open file dialog
            if (openDialog.ShowDialog() == DialogResult.Cancel) return;

            // save the path settings
            QuickTuner.Properties.Settings.Default.lastOpenPath =
                Path.GetDirectoryName(openDialog.FileName);

            // load list of stations from the selected file
            var fs = File.OpenRead(openDialog.FileName);

            var importList = StationList.LoadFrom(fs);

            fs.Close();
            fs.Dispose();

            foreach (var station in importList)
                stationsSource.Add(station);
        }
        private void exportListButton_Click(object sender, EventArgs e)
        {
            // load saved path or default to my documents
            var path = QuickTuner.Properties.Settings.Default.lastSavePath;
            if (string.IsNullOrEmpty(path))
                path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            saveDialog.InitialDirectory = path;

            // show the save file dialog
            if (saveDialog.ShowDialog() == DialogResult.Cancel) return;

            // save the path settings
            QuickTuner.Properties.Settings.Default.lastSavePath =
                Path.GetDirectoryName(saveDialog.FileName);

            // save list of stations to selected file
            var fs = File.OpenWrite(saveDialog.FileName);

            _list.SaveTo(fs);

            fs.Close();
            fs.Dispose();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void editForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // make sure any editing progress is commited before closing the form
            if (stationsGrid.IsCurrentCellInEditMode)
                stationsGrid.EndEdit();
        }

        private void stationsSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            e.NewObject = new Station();
        }

        private void stationsGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            var emptyImage = QuickTuner.Properties.Resources.music;

            foreach (DataGridViewRow row in stationsGrid.Rows) {

                // get the row's attached station
                var station = row.DataBoundItem as Station;
                if (station == null) return;

                // get the first cell
                var cell = row.Cells[0] as DataGridViewImageCell;

                // assign the "empty" image first incase a favicon is not found.
                cell.Value = emptyImage;

                // download image and if success, use it as the icon
                IconProvider.BeginGetIcon(station, (ar) => {
                    var img = IconProvider.EndGetIcon(ar);
                    if (img != null) cell.Value = img;
                });

                Application.DoEvents();
            }
        }

        private void editForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            InstanceRunning = false;
        }
    }


}
